<template>
  <div>
    <!--引用表格:rowSelection="rowSelection"-->
    <BasicTable @register="registerTable">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" v-auth="'wyf:wyf_pms:add'" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <AccountModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="wyf-wyfPms" setup>
  import { ref, reactive, computed, unref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import AccountModal from './AccountModal.vue';
  import { policeColumns, searchPoliceFormSchema } from '../account.data';
  import { list, policeDeleteOne, updateStatus } from '../account.api';
  import { useUserStore } from '/@/store/modules/user';
  const queryParam = reactive<any>({});
  const checkedKeys = ref<Array<string | number>>([]);
  const userStore = useUserStore();
  //注册model
  const [registerModal, { openModal }] = useModal();
  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '公安账号',
      api: list,
      columns: policeColumns,
      canResize: false,
      formConfig: {
        //labelWidth: 120,
        schemas: searchPoliceFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: [],
      },
      actionColumn: {
        width: 200,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        let gxqy = '';
        if (params.gxqy) {
          let xzqy = params.gxqy.split(',');
          gxqy = xzqy[xzqy.length - 1];
        }

        return Object.assign(params, { gxqy });
      },
      searchInfo: { yhfl: 1 },
      bordered: false,
      scroll: { x: 800 },
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
      type: 'Police',
    });
  }
  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    console.log('record: ', record);
    // 处理 gxqys 字段
    if (record.gxqys && typeof record.gxqys === 'string') {
      record.gxqys = record.gxqys.split(','); // 转为数组
    }
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
      type: 'Police',
    });
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await policeDeleteOne({ id: record.id }, handleSuccess);
  }

  /**
   * 修改状态
   */
  async function handleUpdateStatus(record) {
    await updateStatus({ id: record.id, status: record.status == 1 ? 2 : 1 }, handleSuccess);
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
        //  auth: 'wyf:wyf_pms:edit',
      },
      {
        label: record.status == 1 ? '冻结' : '解冻',
        popConfirm: {
          title: '是否修改状态',
          confirm: handleUpdateStatus.bind(null, record),
          placement: 'topLeft',
        },
        // auth: '',
      },
      // {
      //   label: record.delFlag == 0 ? '禁用' : '启用',
      //   popConfirm: {
      //     title: '是否修改状态',
      //     confirm: handleUpdateStatus.bind(null, record),
      //     placement: 'topLeft',
      //   },
      //   auth: 'wyf:wyf_pms:edit',
      //   ifShow: () => record.id !== 1,
      // },
    ];
  }
</script>
<style scoped lang="less">
  .jeecg-basic-table-form-container {
    padding: 0 !important;
    :deep(.ant-form) {
      margin-bottom: 12px !important;
      padding: 0 !important;
    }
  }
  :deep(.jeecg-basic-table) {
    .ant-table-wrapper {
      padding: 0 0 16px 0;
    }
  }
</style>
